package com.bitcat.firstapp.demo.intentdemo

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bitcat.firstapp.R
import com.bitcat.firstapp.databinding.ActivityFirstBinding
import es.dmoral.toasty.Toasty
import kotlinx.parcelize.Parcelize
import java.io.Serializable

class FirstActivity : AppCompatActivity() {


    private lateinit var binding: ActivityFirstBinding

//    private val launcher =
//        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
//            if (result.resultCode == RESULT_OK) {
//                val name = result.data?.getStringExtra("name")
//                Toasty.success(this, "新的方式：name:$name", Toasty.LENGTH_SHORT).show()
//            }
//        }

    private val launcher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult())
        {

//        if (it.resultCode == RESULT_OK) {
//            val name = it.data?.getStringExtra("name")
//            Toasty.success(this, "新的方式：name:$name", Toasty.LENGTH_SHORT).show()
//        }

                result ->
            if (result.resultCode == RESULT_OK) {
                val name = result.data?.getStringExtra("name")
                Toasty.success(this, "新的方式：name:$name", Toasty.LENGTH_SHORT).show()
            }

        }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFirstBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }


    //跳转到第二个页面
    fun jump1(view: View) {
        val intent = Intent(this, SecondActivity::class.java)
        startActivity(intent)
    }


    //普通跳转(传递基本数据类型)
    fun jump2(view: View) {
        val intent = Intent(this, SecondActivity::class.java)
        intent.putExtra("name", "bitcat")
        intent.putExtra("age", 18)
        intent.putExtra("height", 1.88)
        intent.putExtra("is_student", true)
        startActivity(intent)
    }

    //传递对象 (Serializable)
    fun jump3(view: View) {
        val intent = Intent(this, SecondActivity::class.java)
        val person = Person("bitcat", 18)
        intent.putExtra("person", person)
        startActivity(intent)
    }

    //传递对象 (Parcelable)
    fun jump4(view: View) {
        val intent = Intent(this, SecondActivity::class.java)
        val person = Person2("bitcat", 20)
        intent.putExtra("person", person)
        startActivity(intent)
    }


    //第一步：跳转页面
    fun jump5(view: View) {
        val intent = Intent(this, SecondActivity::class.java)
        startActivityForResult(intent, 100)
    }


    //第三步：接收数据
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == RESULT_OK) {
            val name = data?.getStringExtra("name")
            Toasty.success(this, "name:$name", Toasty.LENGTH_SHORT).show()
        }
    }

    fun jump6(view: View) {
        val intent = Intent(this, SecondActivity::class.java)
        launcher.launch(intent)
    }


}

data class Person(val name: String, val age: Int) : Serializable

@Parcelize
data class Person2(val name: String, val age: Int) : Parcelable